<template>
  <view class="user-detail-container">
    <!-- 顶部导航 -->
    <view class="navbar">
      <text class="back-btn" @tap="goBack">‹</text>
      <text class="title">用户详情</text>
    </view>
    
    <!-- 用户信息卡片 -->
    <view class="user-card" v-if="userInfo">
      <view class="user-avatar">
        {{ userInfo.username ? userInfo.username.charAt(0).toUpperCase() : 'U' }}
      </view>
      <view class="user-name">{{ userInfo.username || '未知用户' }}</view>
      <view class="user-dept">{{ userInfo.deptName || '无部门' }}</view>
    </view>
    
    <!-- 详细信息 -->
    <view class="info-list" v-if="userInfo">
      <view class="info-item">
        <text class="info-label">用户名</text>
        <text class="info-value">{{ userInfo.username || '-' }}</text>
      </view>
      <view class="info-item">
        <text class="info-label">部门</text>
        <text class="info-value">{{ userInfo.deptName || '-' }}</text>
      </view>
      <view class="info-item">
        <text class="info-label">职位</text>
        <text class="info-value">{{ userInfo.position || '-' }}</text>
      </view>
      <view class="info-item">
        <text class="info-label">邮箱</text>
        <text class="info-value">{{ userInfo.email || '-' }}</text>
      </view>
      <view class="info-item">
        <text class="info-label">手机号</text>
        <text class="info-value">{{ userInfo.mobile || '-' }}</text>
      </view>
    </view>
    
    <!-- 操作按钮 -->
    <view class="action-buttons" v-if="userInfo">
      <button class="action-btn primary" @tap="startChat">
        <text class="btn-text">发消息</text>
      </button>
    </view>
    
    <!-- 加载状态 -->
    <view v-if="loading" class="loading-mask">
      <text class="loading-text">加载中...</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userId: '',
      userInfo: null,
      loading: false
    }
  },
  
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    
    // 获取用户详情
    async loadUserDetail() {
      this.loading = true
      try {
        const response = await this.$api.system.user.getUserProfile(this.userId)
        if (response.code === 200) {
          this.userInfo = response.data || {}
        } else {
          uni.showToast({
            title: response.msg || '获取用户详情失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('加载用户详情失败:', error)
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },
    
    // 开始聊天
    startChat() {
      if (this.userInfo && this.userInfo.id) {
        uni.navigateTo({
          url: `/pages/chat/index?contactId=${this.userInfo.id}&contactName=${this.userInfo.username}`
        })
      }
    }
  },
  
  onLoad(options) {
    this.userId = options.id || ''
    this.loadUserDetail()
  }
}
</script>

<style lang="scss" scoped>
.user-detail-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.navbar {
  display: flex;
  align-items: center;
  background-color: #fff;
  padding: 30rpx;
  border-bottom: 1rpx solid #eee;
}

.back-btn {
  font-size: 40rpx;
  color: #333;
  margin-right: 30rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.user-card {
  background-color: #fff;
  padding: 60rpx 30rpx;
  text-align: center;
  margin-bottom: 20rpx;
}

.user-avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  background-color: #1890ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 48rpx;
  font-weight: bold;
  margin: 0 auto 30rpx;
}

.user-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 15rpx;
}

.user-dept {
  font-size: 28rpx;
  color: #666;
}

.info-list {
  background-color: #fff;
  margin-bottom: 20rpx;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f5f5f5;
  
  &:last-child {
    border-bottom: none;
  }
}

.info-label {
  font-size: 30rpx;
  color: #333;
}

.info-value {
  font-size: 30rpx;
  color: #666;
}

.action-buttons {
  padding: 30rpx;
}

.action-btn {
  width: 100%;
  height: 80rpx;
  border-radius: 40rpx;
  font-size: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  
  &.primary {
    background-color: #1890ff;
    color: #fff;
  }
}

.btn-text {
  font-size: 32rpx;
}

.loading-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.loading-text {
  color: #666;
  font-size: 28rpx;
}
</style>